package com.jdbcdemo.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DBconnect {
	String name = "";
	String pass = "";
	String driveclass = "";
	String url = "";
	Connection conn = null;
	PreparedStatement pst = null;
	Properties properties = new Properties();

	public DBconnect() throws IOException, ClassNotFoundException {
		FileInputStream fis = new FileInputStream("db.properties");
		properties.load(fis);
		name = properties.getProperty("name");
		pass = properties.getProperty("pass");
		driveclass = properties.getProperty("driverClassName");
		url = properties.getProperty("url");
		Class.forName(driveclass);
	}

	public Connection getConn() throws SQLException, ClassNotFoundException {

		if (conn == null) {
			try {
				conn = DriverManager.getConnection(url, name, pass);
				conn.setAutoCommit(false);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return conn;
	}
}
